<?php
class IndexAction extends CommonAction {

	private $Ad;
	private $User;

	public function __construct(){
		parent::__construct();
		$this->Ad = D('Ad');
		$this->User = D('User');
	}
	//空操作
	Public function _empty(){
	    header("HTTP/1.0 404 Not Found");//使HTTP返回404状态码
	    $this->display("Public:404");
	}
    public function index(){

        if(chkStr($_GET['invit'])){
		    $user = $this->User->where('invit=\''.$_GET['invit'].'\'')->find();
			if($user) $_SESSION['invit'] = $_GET['invit'];
		}
// 		//算力排行
//     	$sql='SELECT SUM(allsuanli) AS allsuanli,a.user as id,a.username FROM(SELECT t_factory.suanli*t_factory_user.num AS allsuanli, t_user.username, t_factory.suanli, t_factory_user.id, t_factory_user.factory,t_factory_user.`user`,t_factory_user.num FROM `t_factory_user` JOIN t_factory ON t_factory_user.factory=t_factory.id JOIN t_user ON t_factory_user.`user`=t_user.id WHERE t_factory_user.status=1) as a GROUP BY USER ORDER BY allsuanli DESC';
//     	$list=M()->query($sql);
// 		//print_r($list);
//         $this->assign('buy',$list);
//         $sql='SELECT sum(j.money) as jiangli,j.upuser,u.id,u.username as username  FROM `t_factory_jiangli` as j JOIN t_user as u ON j.upuser=u.id  GROUP BY j.upuser order by jiangli desc';
//         $jiangli=M()->query($sql);
//         foreach($jiangli as $k=> $v){
//             $r=M('User')->where("id={$v['username']}")->find();
//             $jiangli[$k]['username']=substr($v['username'],0,1).'****'.substr($v['username'],-1,1);
//         }
//         $this->assign('jiangli',$jiangli);
//         //推广人数
//         $sql="SELECT COUNT(*) as num,invitup FROM `t_user` WHERE invitup!='' GROUP BY invitup order by num desc";
//         $list=M()->query($sql);
//         foreach ($list as $k =>$v){
//             $user=M('User')->where("invit={$v['invitup']}")->find();
//             $list[$k]['id']=$user['invit'];
//         }
//         $this->assign('tuiguang',$list);
        if (!empty($_SESSION['USER_KEY_ID'])){
            $User = D('User');
           $user = $User->where('id='.$_SESSION['USER_KEY_ID'])->lock(true)->find();
            $this->assign('userinfo',$user);
        }
      ;
        $this->sys = M('Sys')->where('id=1')->find();
        $this->assign('sys',$this->sys);
		$coin = chkStr($_GET['coin']) ? $_GET['coin'] : 'kg';
		$this->assign('coin',$coin);

		$ad = $this->Ad->order('id asc')->select();
		$this->assign('ad',$ad);

		$_SESSION['back_url'] = __SELF__=='/'?$this->path.__ACTION__:__SELF__;
        $this->assign('notlogin',$_SESSION['notlogin']);

		$_SESSION['notlogin']=0;
		$this->assign('back_url',$_SESSION['back_url']);
        $this->assign('user',$_SESSION['USER_KEY_ID']);
        
        $usercount=M("User")->count();
        $this->assign("usercount",$usercount);
        $trans=M("Trans")->sum("num*price");
        $num=unpack("c*", intval($trans));
        foreach ($num as $v){
        	$tt[]=$this->list_int($v);
        }
        $this->assign("num",$tt);
        $this->assign("trans",intval($trans));
        
       //**  幻灯片
      $flash=M("Flash")->limit(3)->select();
      $this->assign("flash",$flash);
        
        
      /**
       * 下面的广告
       */  
      $huiyuan=M("Art")->where("cate='huiyuan'")->limit(3)->order("ctime desc")->select();//会员
      $changjianwenti=M("Art")->where("cate='changjianwenti'")->limit(3)->order("ctime desc")->select();//常见问题
      $newsdongtai=M("Art")->where("cate='newsdongtai'")->limit(3)->order("ctime desc")->select();//最新动态
      
    
     $this->assign("huiyuan",$huiyuan);
     $this->assign("changjianwenti",$changjianwenti);
     $this->assign("newsdongtai",$newsdongtai);
      
  
		$this->display('./Tpl/Home/Index.html');
    }
    private function list_int($num){
    	switch ($num){
    		case 48;$name=0;break;
    		case 49;$name=1;break;
    		case 50;$name=2;break;
    		case 51;$name=3;break;
    		case 52;$name=4;break;
    		case 53;$name=5;break;
    		case 54;$name=6;break;
    		case 55;$name=7;break;
    		case 56;$name=8;break;
    		case 57;$name=9;break;
    		default:$name="";
    	}
    	return $name;
    }
}